package com.shijie.log.util;

import com.shijie.log.formatter.ValueFormatter;
import com.shijie.log.recorder.DiffLogRecorder;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 变更日志记录工具类：支持传递格式化器
 */
@Slf4j
public class DiffLogRecordUtil {
    private DiffLogRecordUtil() {
    }

    // ---------------------- 批量记录（带格式化器） ----------------------
    public static <T> void batchRecord(DiffLogRecorder<T> recorder, List<T> changedFields) {
        if (recorder == null) {
            log.error("日志记录器未指定，记录失败");
            throw new IllegalArgumentException("DiffLogRecorder cannot be null");
        }
        if (changedFields == null || changedFields.isEmpty()) {
            return;
        }
        try {
            recorder.record(changedFields);
        } catch (Exception e) {
            log.error("批量日志记录失败");
            throw new RuntimeException("批量日志记录异常", e);
        }
    }

    // ---------------------- 单字段记录（带格式化器） ----------------------
    public static <T> void record(DiffLogRecorder<T> recorder, String key, Object oldVal, Object newVal, ValueFormatter formatter) {
        if (recorder == null) {
            log.error("日志记录器未指定，记录失败");
            throw new IllegalArgumentException("DiffLogRecorder cannot be null");
        }
        try {
            recorder.record(key, oldVal, newVal, formatter);
        } catch (Exception e) {
            log.error("单字段日志记录失败");
            throw new RuntimeException("单字段日志记录异常", e);
        }
    }

}